package com.example.zzl.model.HomeFragment;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.example.zzl.model.BaseModel;
import com.example.zzl.model.bean.Info;
import com.example.zzl.model.bean.InfoFoot;
import com.example.zzl.model.cans.HomeUrl;
import com.example.zzl.utils.specification.VolleySingleton;
import com.example.zzl.viewmodel.BaseViewModel;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by ligg on 2017/2/14.
 */

public class InfoModel extends BaseModel implements Response.Listener<String> {

    public Info getInfo() {
        return info;
    }

    public List<InfoFoot> getList() {
        return list;
    }

    private Info info;
    private List<InfoFoot> list;

    public InfoModel(Context context, BaseViewModel callback) {
        super(context, callback);
    }

    @Override
    public void loadData(String parameter) {
        StringRequest stringRequest = new StringRequest(HomeUrl.info + parameter, this, VolleySingleton.volleySingleton);
        StringRequest stringRequest1 = new StringRequest(HomeUrl.mayLikeInfo + parameter, this, VolleySingleton.volleySingleton);
        RequestQueue requestQueue = VolleySingleton.volleySingleton.getRequestQueue();
        requestQueue.add(stringRequest);
        requestQueue.add(stringRequest1);
    }

    @Override
    public void onResponse(String response) {
        Gson gson = new Gson();
        if (response.startsWith("[")) {
            Type type = new TypeToken<List<InfoFoot>>() {
            }.getType();
            list = gson.fromJson(response, type);
            callback.loadFinished();
        } else {
            info = gson.fromJson(response, Info.class);
            callback.loadFinished();
        }
    }
}
